package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.dao.OmUserIndustryDao;
import com.bestcem.xm.om.dao.mapper.OmUserIndustryMapper;
import com.bestcem.xm.om.entity.OmUserIndustry;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/27 16:17
 */
@Repository
public class OmUserIndustryDaoImpl implements OmUserIndustryDao {
    @Resource
    private OmUserIndustryMapper omUserIndustryMapper;

    @Override
    public List<OmUserIndustry> listAll(Integer page, Integer size) {
        Integer start = null;
        if (Objects.nonNull(page) && Objects.nonNull(size)) {
            start = (page - 1) * size;
        }
        return omUserIndustryMapper.listAll(start, size);
    }

    @Override
    public List<OmUserIndustry> findByOmUserId(String omUserId) {
        return omUserIndustryMapper.findByOmUserId(omUserId);
    }

    @Override
    public List<OmUserIndustry> listByIndustryId(String industryId) {
        return omUserIndustryMapper.listByIndustryId(industryId);
    }

    @Override
    public OmUserIndustry findOneByIndustryId(String industryId) {
        return omUserIndustryMapper.findOneByIndustryId(industryId);
    }

    @Override
    public List<OmUserIndustry> listByCondition(OmUserIndustry userIndustry) {
        return omUserIndustryMapper.listByCondition(userIndustry);
    }

    @Override
    public List<OmUserIndustry> listAllIndustry() {
        return omUserIndustryMapper.listAll(null, null);
    }

    @Override
    public List<OmUserIndustry> listByIds(List<String> ids) {
        return omUserIndustryMapper.listByIds(ids);
    }

    @Override
    public void batchSave(List<OmUserIndustry> omUserIndustries) {
        LocalDateTime now = LocalDateTime.now();
        for (OmUserIndustry omUserIndustry : omUserIndustries) {
            omUserIndustry.setCreateTime(now);
            omUserIndustry.setUpdateTime(now);
            omUserIndustry.setId(ObjectId.next());
        }
        omUserIndustryMapper.batchSave(omUserIndustries);
    }

    @Override
    public Integer deleteByUserId(String userId) {
        return omUserIndustryMapper.deleteByUserId(userId);
    }

    @Override
    public boolean existsByOmUserIdAndIndustryId(String omUserId, String industryId) {
        return omUserIndustryMapper.existsByOmUserIdAndIndustryId(omUserId, industryId) != 0;
    }

    @Override
    public List<OmUserIndustry> listByUserIds(List<String> omUserIds) {
        return omUserIndustryMapper.listByUserIds(omUserIds);
    }
}
